package com.eagledata.project.business.banner.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.eagledata.common.exception.file.FileNameLengthLimitExceededException;
import com.eagledata.common.utils.FileUploadUtils;
import com.eagledata.framework.config.EagleDataConfig;
import com.eagledata.framework.web.controller.BaseController;
import com.eagledata.framework.web.domain.Message;
import com.eagledata.framework.web.page.TableDataInfo;
import com.eagledata.project.business.banner.domain.Banner;
import com.eagledata.project.business.banner.service.IBannerService;

/**
 * 轮播图 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-06-22
 */
@Controller
@RequestMapping("/business/banner")
public class BannerController extends BaseController {
	private String prefix = "business/banner";

	@Autowired
	private IBannerService bannerService;

	@RequestMapping()
	@RequiresPermissions("module:banner:view")
	public String banner() {
		return prefix + "/banner";
	}

	/**
	 * 
	 * 查询轮播图列表
	 */
	@RequiresPermissions("module:banner:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(HttpServletRequest request, Banner banner) {
		startPage();
		List<Banner> list = bannerService.selectBannerList(banner);
		return getDataTable(list);
	}

	/**
	 * 新增轮播图
	 */
	@RequiresPermissions("module:banner:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 修改轮播图
	 */
	@RequiresPermissions("module:banner:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		Banner banner = bannerService.selectBannerById(id);
		model.addAttribute("banner", banner);
		model.addAttribute("imageUrl", EagleDataConfig.getBaseImgUrl()+ banner.getBannerUrl());
		return prefix + "/edit";
	}

	/**
	 * 保存轮播图
	 */
	@RequiresPermissions("module:banner:save")
	@PostMapping("/save")
	@ResponseBody
	public Message save(MultipartFile file, Banner banner) {
		if(null!=file) {
			try {
				String baseDir = EagleDataConfig.getProfile();
				String extension = FileUploadUtils.getFileSuffix(file.getOriginalFilename());
				String fileName = FileUploadUtils.upload(baseDir, file, extension);
				banner.setBannerUrl(fileName);
			} catch (FileSizeLimitExceededException e) {
				e.printStackTrace();
			} catch (FileNameLengthLimitExceededException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (bannerService.saveBanner(banner) > 0) {
			return Message.success();
		}
		return Message.error();
	}

	/**
	 * 删除轮播图
	 */
	@RequiresPermissions("module:banner:remove")
	@PostMapping("/remove/{id}")
	@ResponseBody
	public Message remove(@PathVariable("id") Integer id) {
		if (bannerService.deleteBannerById(id) > 0) {
			return Message.success();
		}
		return Message.error();
	}

	/**
	 * 批量删除轮播图
	 */
	@RequiresPermissions("module:banner:batchRemove")
	@PostMapping("/batchRemove")
	@ResponseBody
	public Message remove(@RequestParam("ids[]") Integer[] ids) {
		int rows = bannerService.batchDeleteBanner(ids);
		if (rows > 0) {
			return Message.success();
		}
		return Message.error();
	}

}
